package user;

import utils.JsonOut;
import utils.MysqlUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

@WebServlet(name = "SearchUserServlet")
public class SearchUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        String email = UserSession.getUserSessionEmail(session);

        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-type", "text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

//        if (email == null || email.isEmpty()){
//            out.println(JsonOut.error(220, "not_login"));
//            return;
//        }

        String keyword = request.getParameter("keyword");

        if (keyword == null || keyword.isEmpty()){
            out.println(JsonOut.error(201, "no_keyword"));
            return;
        }

        String[] fbsArr = { "\\", "\"", "'", "<", ">" };
        for (String key : fbsArr) {
            if (keyword.contains(key)) {
                keyword = keyword.replace(key, "\\" + key);
            }
        }

        String fromNum = request.getParameter("m");
        String toNum = request.getParameter("n");

        if (fromNum == null || fromNum.isEmpty()) {
            fromNum = "0";
        }

        if (toNum == null || toNum.isEmpty()){
            toNum = "10";
        }

        int fromInt = 0;
        int toInt = 10;

        try{
            fromInt = Integer.parseInt(fromNum);
            toInt = Integer.parseInt(toNum);
            if (toInt < fromInt) toInt = fromInt;
        }catch (Exception e){
            e.printStackTrace();
        }

        ResultSet ret = (new MysqlUtils()).searchUser(keyword, fromInt, toInt);

        try {
            ArrayList<HashMap<String, String>> array = new ArrayList<>();
            while (ret.next()){
                HashMap<String, String> item = new HashMap<>();

                // id,nickname,area,sex,head_image
                String userId = ret.getString(1);
                String nickname = ret.getString(2);
                String area = ret.getString(3);
                String sex = ret.getString(4);
                String headImage = ret.getString(5);
                String birthday = ret.getString(6);
                String signature = ret.getString(7);
                item.put("user_id", userId != null ? userId : "");
                item.put("nickname", nickname != null ? nickname : "");
                item.put("area", area != null ? area : "");
                item.put("sex", sex != null ? sex : "");
                item.put("head_image", headImage != null ? headImage : "");
                item.put("birthday", birthday != null ? birthday : "");
                item.put("signature", signature != null ? signature : "");

                if (email != null && !email.isEmpty()) {
                    String followState = (new MysqlUtils()).getFollowStatus(email, userId);
                    item.put("follow_state", followState != null ? followState : "");
                }
                array.add(item);
            }

            out.println(JsonOut.common(0, "search_result", array));
        } catch (Exception e) {
            e.printStackTrace();
            out.println(JsonOut.error(500, "sql_sth_wrong"));
        }
    }
}
